﻿using InteControlSys.SampleService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using PropertyTools.Wpf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;

namespace InteControlSys.ViewModel
{
    public class TrainArriveHisViewModel:NodeViewModel
    {
         public SampleService.SampleClient sp = new SampleClient("WSHttpBinding_ISample");
        public SampleCommon sc = new SampleCommon();

        public TrainArriveHisViewModel(string name)
        {
            this.Name = name;
            this.Parent = null;
            this.IsExpanded = true;
            this.NodeType = "TrainArriveHisViewModel";
            this.OperatingTruckList = new ObservableCollection<NodeViewModel>();

            Init();
         }

        public TrainArriveHisViewModel()
        {
            Init();
        }
        public override Uri GetMyIcon()
        {
            return new Uri("../Resources/Images/bucket.png", UriKind.RelativeOrAbsolute);
        }

        #region Properties
        private DateTime _dpBegin;

        public DateTime dpBegin
        {
            get { return _dpBegin; }
            set
            {
                _dpBegin = value;
                NotifyChanged("dpBegin");
            }
        }

        private DateTime _dpEnd;

        public DateTime dpEnd
        {
            get { return _dpEnd; }
            set
            {
                _dpEnd = value;
                NotifyChanged("dpEnd");
            }
        }

        private bool _isMaterial;

        public bool IsMaterial
        {
            get { return _isMaterial; }

            set
            {
                _isMaterial = value;
                NotifyChanged("IsMaterial");
                GetCondition();
            }
        }

        private bool _isSupplier;

        public bool IsSupplier
        {
            get { return _isSupplier; }

            set
            {
                _isSupplier = value;
                NotifyChanged("IsSupplier");
                GetCondition();
            }
        }

        private bool _isPOrder;

        public bool IsPOrder
        {
            get { return _isPOrder; }

            set
            {
                _isPOrder = value;
                NotifyChanged("IsPOrder");

            }
        }

        private List<PurchaseOrderInfo> purchaseList;
        public List<PurchaseOrderInfo> PurchaseList
        {
            get { return purchaseList; }
            set
            {
                purchaseList = value;
                NotifyChanged("PurchaseList");
            }
        }

        private List<SupplierInfo> supplierList;
        public List<SupplierInfo> SupplierList
        {
            get { return supplierList; }
            set
            {
                supplierList = value;
                NotifyChanged("SupplierList");
            }
        }

        private List<MaterielInfo> materialList;
        public List<MaterielInfo> MaterialList
        {
            get { return materialList; }
            set
            {
                materialList = value;
                NotifyChanged("MaterialList");
            }
        }

        private MaterielInfo _selectMaterial;
        public MaterielInfo SelectMaterial
        {
            get { return _selectMaterial; }
            set
            {
                _selectMaterial = value;
                NotifyChanged("SelectMaterial");
                GetCondition();
            }
        }

        private SupplierInfo _selectSupplier;
        public SupplierInfo SelectSupplier
        {
            get { return _selectSupplier; }
            set
            {
                _selectSupplier = value;
                NotifyChanged("SelectSupplier");
                GetCondition();
            }
        } 
        
        private PurchaseOrderInfo _selectPOrder;
        public PurchaseOrderInfo SelectPOrder
        {
            get { return _selectPOrder; }
            set
            {
                _selectPOrder = value;
                NotifyChanged("SelectPOrder");

            }
        }

        private ObservableCollection<TrainArrive> trainArriveList;
        public ObservableCollection<TrainArrive> TrainArriveList
        {
            get { return trainArriveList; }
            set
            {
                trainArriveList = value;
                NotifyChanged("TrainArriveList");
            }
        }
        #endregion

        public void GetCondition()
        {
            //this.MaterialList = sp.AllMaterial().ToList();
            //this.SupplierList = sp.ListAllSuppliers().ToList();
            Guid? mid, sid;
            if (SelectMaterial != null)
                mid = SelectMaterial.ID;
            else mid = null;
            if (SelectSupplier != null)
                sid = SelectSupplier.ID;
            else
                sid = null;
            this.PurchaseList = sp.GetPOrderWithDate(dpBegin, dpEnd, IsMaterial, mid, IsSupplier, sid).ToList();

        }

        public void Init()
        {
            dpBegin = DateTime.Now.AddDays(-7);
            dpEnd = DateTime.Now;
            this.MaterialList = sp.AllMaterial().ToList();
            this.SupplierList = sp.ListAllSuppliers().ToList();
            Guid? mid, sid;
            if (SelectMaterial != null)
                mid = SelectMaterial.ID;
            else mid = null;
            if (SelectSupplier != null)
                sid = SelectSupplier.ID;
            else
                sid = null;
      
            var list = sp.GetPOrderWithDate(dpBegin, dpEnd, IsMaterial, mid, IsSupplier, sid);
            if(list!=null)
                this.PurchaseList = list.ToList();
            this.IsMaterial = false;
            this.IsSupplier = false;
            this.IsPOrder = false;
           
            //GetList();
        }

        public void GetList()
        {
            Guid? mid,sid,pid;
            if (SelectMaterial != null)
                mid = SelectMaterial.ID;
            else mid = null;
            if (SelectSupplier != null)
                sid = SelectSupplier.ID;
            else
                sid = null;
            if (SelectPOrder != null)
                pid = SelectPOrder.ID;
            else
                pid = null;

            var list = sp.GetShipDocWithConditions(dpBegin, dpEnd, IsMaterial, mid, IsSupplier, sid, IsPOrder, pid);
            trainArriveList = new ObservableCollection<TrainArrive>();
            foreach (var item in list)
            {
                var ta = new TrainArrive();
                ta.POrderID = item.PurchaseOrder.PurchaseOrderID;
                ta.StrMateriel = item.PurchaseOrder.Materiel.Name;
                ta.StrSupplier = item.PurchaseOrder.Supplier.SupplierName;
                if (item.Conveyance != null)
                    ta.StrCarrageNo = item.Conveyance.Identity;
                ta.StrStatus = sc.DicDiliverStatus[item.DeliverStatus];
                ta.ReceiveTime = item.ReceiveTime;
                ta.ReceiveLocation = item.ReceiveLocation;
                trainArriveList.Add(ta);
            }
            this.TrainArriveList = trainArriveList;
        }

        public ICommand Query
        {
            get
            {
                return new DelegateCommand(() =>
                {
                    GetList();
                });
            }
        }


    }

    public class TrainArrive : ShipDocInfo
    {
        public TrainArrive() { }

        public string POrderID { get; set; }

        public string StrMateriel { get; set; }

        public string StrSupplier { get; set; }

        public string StrCarrageNo { get; set; }

        public string StrStatus { get; set;}
    }
}
